//
//  AppDelegate.m
//  JTY_App
//
//  Created by jty on 2025/3/29.
//

#import "AppDelegate.h"
#import "JTY_LoginVC.h"

@interface AppDelegate ()


/*!
 *  @brief  异步串行 队列
 */
@property (strong, nonatomic) dispatch_queue_t GCD_AddressBook;

@end

@implementation AppDelegate

-(dispatch_queue_t)GCD_AddressBook {
    if (!_GCD_AddressBook) {
        _GCD_AddressBook = dispatch_queue_create("GCD_AddressBook", DISPATCH_QUEUE_SERIAL);
    }
    return _GCD_AddressBook;
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    
    [self svPreferrenceConf];
    
    JTY_LoginVC *vc = [[JTY_LoginVC alloc]init];
    self.window.rootViewController = vc;
    
    [self.window makeKeyAndVisible];
    return YES;
}

#pragma mark --- SVProgressHUD 偏好设置

- (void)svPreferrenceConf {

    [SVProgressHUD setBackgroundColor:[[UIColor grayColor] colorWithAlphaComponent:0.9]];
    [SVProgressHUD setMinimumDismissTimeInterval:0.8];
    [SVProgressHUD setFont:[UIFont systemFontOfSize:12]];
    [SVProgressHUD setImageViewSize:CGSizeMake(30, 30)];
    [SVProgressHUD setCornerRadius:5.0];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD setMinimumSize:CGSizeMake(90, 90)];
    
    
}
- (void)fetchContactsAsyncWithCompletion:(ContactsCompletionBlock)completion failure:(ContactsFailureBlock)failure {
    // 保存回调Block
    self.contactsCompletionBlock = completion;
    self.failureBlock = failure;
    
    // 创建通讯录对象
    CNContactStore *contactStore = [[CNContactStore alloc] init];
    
    // 请求访问通讯录权限
    [contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted) {
            dispatch_async(self.GCD_AddressBook, ^{
                // 获取通讯录权限成功
                NSArray<CNContact *> *contacts = [self fetchAllContactsFromContactStore:contactStore];
                
                // 调用完成Block，并传递通讯录信息
                if (self.contactsCompletionBlock) {
                    self.contactsCompletionBlock(contacts);
                }
            });
        } else {
            // 请求通讯录权限失败
            if (self.failureBlock) {
                self.failureBlock(error);
            }
        }
    }];
}
- (NSArray<CNContact *> *)fetchAllContactsFromContactStore:(CNContactStore *)contactStore {
    // 创建通讯录信息的请求对象
    NSArray*keysToFetch =@[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactPhoneNumbersKey];
    
    CNContactFetchRequest*fetchRequest = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];
    
    NSMutableArray<CNContact *> *contacts = [NSMutableArray array];
   
    // 获取通讯录信息
    [contactStore enumerateContactsWithFetchRequest:fetchRequest error:nil usingBlock:^(CNContact * _Nonnull contact, BOOL * _Nonnull stop) {
        [contacts addObject:contact];
    }];
    
    return contacts;
}

@end
